
//
//  ViewEngine.m
//  MeicaiCrm
//
//  Created by zhou jianbang on 2019/4/16.
//  Copyright © 2019 meicai. All rights reserved.
//

#import "ViewEngine.h"
#import "AlertView.h"
#import "ToastView.h"
#import "SheetView.h"
#import <Masonry/Masonry.h>
#import <MCFoundation/MCFoundation.h>

@interface ViewEngine()
{
    SheetView * _sheetView;
    ToastView * _loadingView;
}
@property (nonatomic, strong) UIView * coverView;
@property (nonatomic, strong) UIView * contentView;
@end

@implementation ViewEngine

+(ViewEngine *)shareManager
{
    static ViewEngine * tool = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        tool = [[ViewEngine alloc] init];
    });
    return tool;
}

#pragma  mark - alertView
-(NSDictionary *)dataDict
{
    NSDictionary * dataDict = @{
                                @"direction":@"row",
                                @"titleDict":@{
                                        @"title":@"提示",
                                        @"fontSize": @(16),
                                        @"color":@"#333333",
                                        @"bold":@(1),
                                        @"textAlign":@"center",
                                        @"numberOfLines":@(1)
                                        },
                                @"msgDict":@{
                                        @"msg":@"我是测试信息我说的事实上那么挨个看哈感康卡噶科技噶卡机看",
                                        @"fontSize": @(14),
                                        @"color":@"#333333",
                                        @"bold":@(0),
                                        @"textAlign":@"center",
                                        @"numberOfLines":@(2)
                                        },
                                @"buttons":@[
                                        @{
                                            @"name":@"取消",
                                            @"fontSize": @(16),
                                            @"textColor":@"#999999",
                                            @"bold":@(1),
                                            @"key":@"cancel",
                                            @"backgroundColor":@"#FFFFFFF",
                                            },
                                        @{
                                            @"name":@"确定",
                                            @"fontSize": @(16),
                                            @"textColor":@"#3072F6",
                                            @"bold":@(1),
                                            @"key":@"confirm",
                                            @"backgroundColor":@"#FFFFFF",
                                            },
                                        @{
                                            @"name":@"确定",
                                            @"fontSize": @(16),
                                            @"textColor":@"#3072F6",
                                            @"bold":@(1),
                                            @"key":@"confirm",
                                            @"backgroundColor":@"#FFFFFF",
                                            }
                                        ]
                                };
    return  dataDict;
    
}
-(void)addAlertViewWithDict:(NSDictionary *)dict
{
    AlertView * alert = [[AlertView alloc]initWithDataDict:dict andBlock:^(NSDictionary * res) {
        if(self.block) self.block(res);
    }];
    [self.contentView addSubview:alert];
    CGFloat width = [UIScreen mainScreen].bounds.size.width-100*([UIScreen mainScreen].bounds.size.width/375);
    [alert mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(width);
        make.centerY.mas_equalTo(self.contentView.mas_centerY);
        make.centerX.mas_equalTo(self.contentView.mas_centerX);
    }];
}
-(void)showAlertWithBlock:(ActionBlock)block andDict:(NSDictionary *)dict
{
    _block = block;
    UIWindow * window = [UIApplication sharedApplication].keyWindow;
    [self addAlertViewWithDict:dict];
    [window addSubview:self.contentView];
    self.contentView.hidden = NO;
    [UIView animateWithDuration:0.25 animations:^{
        self.contentView.alpha = 1;
        self.coverView.alpha = 0.4;
    } completion:^(BOOL finished) {
      
    }];
}
-(void)hideAlert{
    [UIView animateWithDuration:0.25 animations:^{
        self.coverView.alpha = 0;
        self.contentView.alpha = 0;
    } completion:^(BOOL finished) {
        self.contentView.hidden = YES;
        [self.contentView removeFromSuperview];
        self.contentView = nil;
    }];
}

#pragma  mark - setter
-(UIView *)coverView
{
    if (!_coverView) {
        _coverView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width,  [UIScreen mainScreen].bounds.size.height)];
        _coverView.backgroundColor = [UIColor blackColor];
        _coverView.alpha = 0;
    }
    return _coverView;
}
-(UIView *)contentView
{
    if(!_contentView){
        _contentView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width,  [UIScreen mainScreen].bounds.size.height)];
        _contentView.alpha = 0;
        _contentView.hidden = YES;
        [_contentView addSubview:self.coverView];
    }
    return _contentView;
}
#pragma  mark - toastView

-(NSDictionary *)toastData
{
    NSDictionary * data = @{
                            @"type":@"success",
                            @"msg":@"提交成功",
                            @"duration":@(3)
                            };
    return data;
}
-(void)addToastViewWithDict:(NSDictionary *)dict
{
    ToastView * toastView = [[ToastView alloc]initWithDataDict:dict];
    [self.contentView addSubview:toastView];
    NSString * type = [dict mcObjectForKey:@"type"];
    
    if([type isEqualToString:@"info"]){
        [toastView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(self.contentView.mas_centerY);
            make.centerX.mas_equalTo(self.contentView.mas_centerX);
        }];
    }else{
        [toastView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(self.contentView.mas_centerY);
            make.centerX.mas_equalTo(self.contentView.mas_centerX);
            make.width.mas_equalTo(120);
            make.height.mas_equalTo(120);
        }];
    }
}
-(void)showToastWithBlock:(ActionBlock)block andDict:(NSDictionary *)dict
{
    _toastBlock = block;
    NSNumber * duration = [dict mcObjectForKey:@"duration"];
    NSString * type = [dict mcObjectForKey:@"type"];
    if([NSString mcIsEmpty:type]) return;
    UIWindow * window = [UIApplication sharedApplication].keyWindow;
    [self addToastViewWithDict:dict];
    [window addSubview:self.contentView];
    self.contentView.hidden = NO;
    [UIView animateWithDuration:0.25 animations:^{
        self.contentView.alpha = 1;
        self.coverView.alpha = 0;
    } completion:^(BOOL finished) {
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(duration.floatValue * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self hideToast];
        });
    }];
}
-(void)hideToast{
    [UIView animateWithDuration:0.25 animations:^{
        self.coverView.alpha = 0;
        self.contentView.alpha = 0;
    } completion:^(BOOL finished) {
        self.contentView.hidden = YES;
        [self.contentView removeFromSuperview];
        self->_contentView = nil;
        if(self.self->_toastBlock) self.toastBlock(@"dismiss");
    }];
}

#pragma  mark - loading
-(NSDictionary *)loadingData
{
    NSDictionary * data = @{
                            @"type":@"loading",
                            @"msg":@"测试中",
                            };
    return data;
}
-(void)addLoadingDict:(NSDictionary *)dict
{
    ToastView * LoadingView = [[ToastView alloc]initWithDataDict:dict];
    [self.contentView addSubview:LoadingView];
    [LoadingView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.contentView.mas_centerY);
        make.centerX.mas_equalTo(self.contentView.mas_centerX);
        make.width.mas_equalTo(120);
        make.height.mas_equalTo(120);
    }];
    _loadingView = LoadingView;
}
-(void)showLoadingAndDict:(NSDictionary *)dict
{
    UIWindow * window = [UIApplication sharedApplication].keyWindow;
    [self addLoadingDict:dict];
    
    NSString * type = [dict mcObjectForKey:@"type"];
    if([NSString mcIsEmpty:type]) return;

    [window addSubview:self.contentView];
    self.contentView.hidden = NO;
    [UIView animateWithDuration:0.25 animations:^{
        self.contentView.alpha = 1;
        self.coverView.alpha = 0;
        [self->_loadingView startLoading];
    } completion:nil];
}
-(void)hideLoading{
    [UIView animateWithDuration:0.25 animations:^{
        self.coverView.alpha = 0;
        self.contentView.alpha = 0;
    } completion:^(BOOL finished) {
        [self->_loadingView stopLoading];
        self.contentView.hidden = YES;
        [self.contentView removeFromSuperview];
        self->_contentView = nil;
    }];
}
#pragma  mark - sheetView

-(NSDictionary *)sheetData
{
    NSDictionary * data = @{
                            @"titleDict":@{
                                    @"title":@"一行牛逼的注释文字",
                                    @"fontSize": @(16),
                                    @"color":@"#999999",
                                    @"bold":@(1),
                                    @"textAlign":@"center",
                                    @"numberOfLines":@(1)
                                    },
                            @"buttons": @[
                                    @{
                                        @"name":@"确定",
                                        @"fontSize": @(16),
                                        @"textColor":@"#3072F6",
                                        @"bold":@(1),
                                        @"key":@"confirm",
                                        @"backgroundColor":@"#FFFFFF",
                                        },
                                    @{
                                        @"name":@"确定",
                                        @"fontSize": @(16),
                                        @"textColor":@"#3072F6",
                                        @"bold":@(1),
                                        @"key":@"confirm",
                                        @"backgroundColor":@"#FFFFFF",
                                        },
                                    @{
                                        @"name":@"确定",
                                        @"fontSize": @(16),
                                        @"textColor":@"#3072F6",
                                        @"bold":@(1),
                                        @"key":@"confirm",
                                        @"backgroundColor":@"#FFFFFF",
                                        },
                                    ]
                            };
    return data;
}

-(void)addSheetViewWithDict:(NSDictionary *)dict
{
    SheetView * sheetView = [[SheetView alloc]initWithDataDict:dict andBlock:^(NSDictionary *res) {
        if(self.sheetBlock) self.sheetBlock(res);
    }];
    
    NSArray * buttons = [dict mcObjectForKey:@"buttons"];
    CGFloat sheetHeight = ((buttons.count+2)*49)+10;
    CGSize size = [UIScreen mainScreen].bounds.size;
    [self.contentView addSubview:sheetView];
    sheetView.frame = CGRectMake(0, size.height, size.width,sheetHeight );
    
    _sheetView = sheetView;
}
-(void)showSheetWithBlock:(ActionBlock)block andDict:(NSDictionary *)dict
{
    _sheetBlock = block;
    CGSize size = [UIScreen mainScreen].bounds.size;
    NSArray * buttons = [[self sheetData] mcObjectForKey:@"buttons"];
    CGFloat sheetHeight = ((buttons.count+2)*49)+10;
    
    UIWindow * window = [UIApplication sharedApplication].keyWindow;
    [self addSheetViewWithDict:dict];
    [window addSubview:self.contentView];
    self.contentView.hidden = NO;
    [UIView animateWithDuration:0.4 animations:^{
        self.contentView.alpha = 1;
        self.coverView.alpha = 0.4;
        self->_sheetView.frame = CGRectMake(0, size.height-sheetHeight, size.width,sheetHeight );
    } completion:^(BOOL finished) {
        
    }];
}
-(void)hideSheetView
{
    CGSize size = [UIScreen mainScreen].bounds.size;
    NSArray * buttons = [[self sheetData] mcObjectForKey:@"buttons"];
    CGFloat sheetHeight = ((buttons.count+2)*49)+10;
    
    [UIView animateWithDuration:0.4 animations:^{
        self.coverView.alpha = 0;
        self.contentView.alpha = 0;
        self->_sheetView.frame = CGRectMake(0, size.height, size.width,sheetHeight );
    } completion:^(BOOL finished) {
        self.contentView.hidden = YES;
        self.contentView = nil;
        [self.contentView removeFromSuperview];
    }];
}

#pragma  mark - loadingView

#pragma  mark - c+++
+(void)alertShowWithBlock:(ActionBlock)block andAlertDict:(NSDictionary *)alertDict
{
    [[ViewEngine shareManager] showAlertWithBlock:block andDict:alertDict];
}
+(void)alertHide
{
    [[ViewEngine shareManager] hideAlert];
}

+(void)toastShowWithBlock:(ActionBlock)block andToastDict:(NSDictionary *)toastDict
{
    [[ViewEngine shareManager] showToastWithBlock:block andDict:toastDict];
}

+(void)sheetShowWithBlock:(ActionBlock)block andSheetDict:(NSDictionary *)sheetDict
{
    [[ViewEngine shareManager] showSheetWithBlock:block andDict:sheetDict];
}
+(void)sheetViewHide
{
    [[ViewEngine shareManager] hideSheetView];
}
+(void)loadingShow:(NSDictionary *)loadingDict
{
    [[ViewEngine shareManager] showLoadingAndDict:loadingDict];
}
+(void)loadingHide
{
    [[ViewEngine shareManager] hideLoading];
}

@end
